package com.paireach.bw.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.paireach.bw.R;
import com.paireach.bw.bean.OrdersBean;

import java.util.List;

/**
 * Created by ${cqc} on 2017/6/19.
 */

public class BackOrderAdapter extends RecyclerView.Adapter {
    private int headerType = 1;
    private int footerType = 2;
    private View headerView;
    private View footerView;

    private List<OrdersBean> list;

    public BackOrderAdapter(List<OrdersBean> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == headerType) {
            return new MyViewHolder(headerView);
        }
        if (viewType == footerType) {
            return new MyViewHolder(footerView);
        }
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_back_origin_end_factory, parent, false);
        return new MyViewHolder(itemView);
    }

    public void setHeaderView(View headerView) {
        this.headerView = headerView;
    }

    public void setFooterView(View footerView) {
        this.footerView = footerView;
    }

    public int getHeaderViewCount() {
        return (headerView == null) ? 0 : 1;
    }
    public int getFooterViewCount() {
        return (footerView == null) ? 0 : 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < getHeaderViewCount()) {
            return headerType;
        }
        if (position >= (list.size() + getHeaderViewCount())) {
            return footerType;
        }
        return super.getItemViewType(position - getHeaderViewCount());
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == headerType) {
            return;
        }
        if (itemViewType == footerType) {
            return;
        }

        MyViewHolder holder = (MyViewHolder) viewHolder;
        OrdersBean info = list.get(position-getHeaderViewCount());
        holder.tv2.setText("交货单号：" + info.getSHPM_NUM());
        holder.tv3.setText("装货地址：" + info.getFRM_SHPG_ADDR());
        holder.tv4.setText("收货地名称：" + info.getTO_SHPG_LOC_NAME());
        holder.tv5.setText("收货方地址：" + info.getTO_SHPG_ADDR());
    }

    @Override
    public int getItemCount() {
        return list.size()+getHeaderViewCount()+getFooterViewCount();
    }


    private class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv2;
        TextView tv3;
        TextView tv4;
        TextView tv5;

        public MyViewHolder(View itemView) {
            super(itemView);
            if (itemView == headerView) {
                return;
            }
            if (itemView == footerView) {
                return;
            }
            tv2 = (TextView) itemView.findViewById(R.id.tv2);
            tv3 = (TextView) itemView.findViewById(R.id.tv3);
            tv4 = (TextView) itemView.findViewById(R.id.tv4);
            tv5 = (TextView) itemView.findViewById(R.id.tv5);
        }
    }
}


